/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface.tools;

import embeddedpaint.model.feature.AImageTool;
import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerManager;
import embeddedpaint.user_interface.tools.dialog.chooser.MyFilechooserSaver;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;


public class ToolsSavePicture extends AImageTool
{
    private static final String M_LABEL_TEXT                    = "Save";
    private static final String M_ICON_PATH                     = "images/save.png";
    private static final int    M_FILE_TYPE_LENGTH              = 3;
    private static final String M_FILE_TYPE_JPG                 = "jpg";
    private static final Color  M_DEFAULT_BACKGROUNG_COLOR_JPG  = Color.BLACK;
    private static final String M_ERROR_MESSAGE_SAVE_TEXT       = "Error during the image saving :\n";
    private static final String M_ERROR_MESSAGE_SAVE_TITLE      = "Saving error";

    public ToolsSavePicture (LayerManager imageManager, JFrame application)
    {
        super(imageManager, application);
    }

    @Override
    public void actionPerformed (ActionEvent e)
    {
        String filePath = this.selectImage();

        if (filePath != null)
        {
            try
            {
                String fileType = filePath.substring(filePath.length() - M_FILE_TYPE_LENGTH);

                BufferedImage image = new BufferedImage(this.m_imageManager.getM_width(), this.m_imageManager.getM_height(), ALayer.M_DEFAULT_IMAGE_TYPE);

                Graphics2D g = (Graphics2D) image.getGraphics();

                this.m_imageManager.drawMe(g);

                g.dispose();

                if (fileType.equals(M_FILE_TYPE_JPG))
                {
                    image = ToolsSavePicture.fillTransparentPixels(image, M_DEFAULT_BACKGROUNG_COLOR_JPG);
                }

                ImageIO.write(image, filePath.substring(filePath.length() - M_FILE_TYPE_LENGTH), new File(filePath));

            }
            catch (IOException ex)
            {
                JOptionPane.showMessageDialog(m_application, M_ERROR_MESSAGE_SAVE_TEXT + ex, M_ERROR_MESSAGE_SAVE_TITLE, JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public static BufferedImage fillTransparentPixels (BufferedImage image, Color fillColor)
    {
        int w = image.getWidth();
        int h = image.getHeight();
        
        BufferedImage image2 = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_RGB);
        
        Graphics2D g = image2.createGraphics();
        
        g.setColor(fillColor);
        g.fillRect(0, 0, w, h);
        g.drawRenderedImage(image, null);
        g.dispose();
        
        return image2;
    }

    @Override
    public String getLabel ()
    {
        return M_LABEL_TEXT;
    }

    @Override
    protected String getIconName ()
    {
        return M_ICON_PATH;
    }

    private String selectImage ()
    {
        JFileChooser fileChooser = new MyFilechooserSaver();

        fileChooser.showSaveDialog(this.m_application);

        if (fileChooser.getSelectedFile() != null)
        {
            FileFilter  selectedFileFilter  = fileChooser.getFileFilter();
            String      path                = fileChooser.getSelectedFile().getPath();
            
            if (path.endsWith(selectedFileFilter.getDescription()))
            {
                return fileChooser.getSelectedFile().getPath();
            }
            else
            {
                return fileChooser.getSelectedFile().getPath() + selectedFileFilter.getDescription();
            }
        }

        return null;
    }
}
